package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.db.DatabaseUtil;
import com.shoujifeng.snowmusic.player.AddToMenuActivity.MyAdapter;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.utils.MusicPlayer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 添加到歌单
 * @author luo
 *
 */
public class EditAddTo extends Activity{
	private ListView listView;
	private View mMoreView;
	private ListAdapter mAdapter;
	private List<HashMap<String, Object>> arrayList;
	private RelativeLayout headView;
	private TextView headText;
	private ImageButton breakBtn;
	private  RelativeLayout myLikeMusic;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_add_to);
		init();
		setList();
	}
	private void init(){
		listView = (ListView) findViewById(R.id.songMenuList);
		listView.setDivider(null);
		mMoreView = LayoutInflater.from(EditAddTo.this)
				.inflate(R.layout.song_menu_add, null);
		listView.addFooterView(mMoreView);
		headView = (RelativeLayout) findViewById(R.id.headLay);
		headText = (TextView) headView.findViewById(R.id.head_text);
		headText.setText("添加到歌单");
		breakBtn = (ImageButton) headView.findViewById(R.id.breakBtn);
		breakBtn.setOnClickListener(onClickListener);
		myLikeMusic = (RelativeLayout) findViewById(R.id.myLikeMusic);
		myLikeMusic.setOnClickListener(onClickListener);
		
		mMoreView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(EditAddTo.this,NewSongMenuActivity.class);
				startActivityForResult(intent, 0); 
			}
		});
		
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				TextView songMenuName = (TextView) view
						.findViewById(R.id.songMenuName);

				DatabaseUtil  databaseUtil = new DatabaseUtil(EditAddTo.this);
				//在播放器中的更多的添加到
				if(ServerAccess.activityName.equals("MusicPlayerActivity")){
					String name =songMenuName.getText().toString();
					String musicName = MusicPlayer.musicInfos.get(MusicPlayer.position).getTitle();
					String playerName = MusicPlayer.musicInfos.get(
							MusicPlayer.position).getArtist();
					long duration    = 	MusicPlayer.musicInfos.get(
							MusicPlayer.position).getDuration();
					long size    = 	 MusicPlayer.musicInfos.get(
							MusicPlayer.position).getSize();
					String url = MusicPlayer.musicInfos.get(
							MusicPlayer.position).getUrl();
					ServerAccess.activityName="";
					databaseUtil.insertMenu(name, musicName, playerName, duration, size, url);
				}
				else{
					List<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>(); 
					list = EditListMusicActivty.mp3list;
					List<String> noList = new ArrayList<String>();
					noList = EditListMusicActivty.noList;
					for (int i = 0; i < EditListMusicActivty.noList.size(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						map = list.get(Integer.valueOf(noList.get(i)));
						String name =songMenuName.getText().toString();
						String musicName =  map.get("title").toString();
						String playerName = map.get("Artist").toString();
						long duration    = 	Long.parseLong(map.get("duration").toString());
						long size    = 	 Long.parseLong(map.get("size").toString());
						String url = map.get("url").toString();
						databaseUtil.insertMenu(name, musicName, playerName, duration, size, url);
					}
				}
				Toast.makeText(EditAddTo.this,
						" 添加成功", Toast.LENGTH_SHORT)
						.show();
				finish();
			
			}
		});
		
	}
	private void setList(){
		DatabaseUtil databaseUtil = new DatabaseUtil(this);
		arrayList = new ArrayList<HashMap<String, Object>>();
		arrayList = databaseUtil.queryMusicMenu();
		mAdapter = new SimpleAdapter(this, arrayList, R.layout.song_menu_list,
				new String[] { "name" }, new int[] { R.id.songMenuName });
		listView.setAdapter(mAdapter);
	}
	
	private View.OnClickListener onClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.breakBtn:{
				finish();
			}
				break;
			case R.id.myLikeMusic:{
				DatabaseUtil  databaseUtil = new DatabaseUtil(EditAddTo.this);
				//在播放器中的更多的添加到
				if(ServerAccess.activityName.equals("MusicPlayerActivity")){
					String name ="我喜欢的歌曲";
					String musicName = MusicPlayer.musicInfos.get(MusicPlayer.position).getTitle();
					String playerName = MusicPlayer.musicInfos.get(
							MusicPlayer.position).getArtist();
					long duration    = 	MusicPlayer.musicInfos.get(
							MusicPlayer.position).getDuration();
					long size    = 	 MusicPlayer.musicInfos.get(
							MusicPlayer.position).getSize();
					String url = MusicPlayer.musicInfos.get(
							MusicPlayer.position).getUrl();
					ServerAccess.activityName="";
					databaseUtil.insertMenu(name, musicName, playerName, duration, size, url);
				}
				else{
					List<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>(); 
					list = EditListMusicActivty.mp3list;
					List<String> noList = new ArrayList<String>();
					noList = EditListMusicActivty.noList;
					for (int i = 0; i < EditListMusicActivty.noList.size(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						map = list.get(Integer.valueOf(noList.get(i)));
						String name ="我喜欢的歌曲";
						String musicName =  map.get("title").toString();
						String playerName = map.get("Artist").toString();
						long duration    = 	Long.parseLong(map.get("duration").toString());
						long size    = 	 Long.parseLong(map.get("size").toString());
						String url = map.get("url").toString();
						databaseUtil.insertMenu(name, musicName, playerName, duration, size, url);
					}
				}
				Toast.makeText(EditAddTo.this,
						" 添加成功", Toast.LENGTH_SHORT)
						.show();
				finish();
			}	
			break;
			
			default:
				break;
			}
		}
	};
	@Override
	protected void onResume() {
		setList();
		super.onRestart();
		
	}
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
	   if (requestCode == 0) { 
		   finish();
	  } 
	} 

	
	
}
